package com.kkb.merchant.center.server.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.merchant.center.api.OrderService;
import com.kkb.merchant.center.common.constant.ConstantKey;
import com.kkb.merchant.center.common.dto.ChartDataDto;
import com.kkb.merchant.center.common.dto.OrderCount;
import com.kkb.merchant.center.common.dto.OrderCountData;
import com.kkb.merchant.center.common.dto.OrderTimeDataDto;
import com.kkb.merchant.center.common.pojo.*;
import com.kkb.merchant.center.common.util.DateTimeUtil;
import com.kkb.merchant.center.server.mappers.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private SubOrderInfoMapper subOrderInfoMapper;


    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderDeliveryMapper orderDeliveryMapper;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Override
    public PageInfo<SubOrderInfo> queryAll(Long shopId, Integer orderStatus, Integer page, Integer size) {
        PageHelper.startPage(page, size);

        Example example = new Example(SubOrderInfo.class);
        if(shopId!=null&&shopId>0){
            example.createCriteria().andEqualTo("shopId",shopId);
        }
        if(orderStatus>=0){
            example.createCriteria().andEqualTo("orderStatus",orderStatus);
        }
        List<SubOrderInfo> subOrderInfos = subOrderInfoMapper.selectByExample(example);
        PageInfo<SubOrderInfo> pageInfo = new PageInfo<>(subOrderInfos);

        return pageInfo;
    }

    @Override
    public List<OrderItem> getDetailInfo(Long orderSubId) {
        Example example = new Example(OrderItem.class);
        example.createCriteria().andEqualTo("subOrderId",orderSubId);
        List<OrderItem> tbOrderItems = orderItemMapper.selectByExample(example);
        return tbOrderItems;
    }

    @Override
    public OrderDelivery getDeliveryInfo(Long orderSubId) {
        Example example = new Example(OrderDelivery.class);
        example.createCriteria().andEqualTo("subOrderId",orderSubId);
        OrderDelivery tbOrderDeliverys = orderDeliveryMapper.selectOneByExample(example);
        return tbOrderDeliverys;
    }

    @Override
    public List<SubOrderInfo> selectAll() {
        return subOrderInfoMapper.selectAll();
    }

    @Override
    public OrderTimeDataDto orderTimeDate(Long shopId) {
        int today = DateTimeUtil.dateToTimestampT(DateUtil.beginOfDay(new Date()));
        int tommorrow = DateTimeUtil.dateToTimestampT(DateUtil.endOfDay(new Date()));
        int yearday = DateTimeUtil.dateToTimestampT(DateUtil.beginOfDay(DateUtil.yesterday()));

        int lastWeek = DateTimeUtil.dateToTimestampT(DateUtil.beginOfDay(DateUtil.lastWeek()));

        int nowMonth = DateTimeUtil.dateToTimestampT(DateUtil.beginOfMonth(new Date()));

        OrderTimeDataDto orderTimeDataDto = new OrderTimeDataDto();

        //今日订单数
        orderTimeDataDto.setTodayCount(subOrderInfoMapper.countBetweenTime(shopId,today,tommorrow));

        //昨日订单数
        orderTimeDataDto.setProCount(subOrderInfoMapper.countBetweenTime(shopId,yearday,today));

        //本周订单数
        orderTimeDataDto.setLastWeekCount(subOrderInfoMapper.countBetweenTime(shopId,lastWeek,tommorrow));

        //本月订单数
        orderTimeDataDto.setMonthCount(subOrderInfoMapper.countBetweenTime(shopId,nowMonth,tommorrow));

        Example example = new Example(MemberInfo.class);
        orderTimeDataDto.setUserCount(memberInfoMapper.selectCountByExample(example));

        Example example1 = new Example(SubOrderInfo.class);
        example1.selectProperties("orderId").setDistinct(true);
        if(shopId>0){
            example1.createCriteria().andEqualTo("shopId",shopId);
        }

        int count = subOrderInfoMapper.selectCountByExample(example1);

        orderTimeDataDto.setOrderCount(count);

        orderTimeDataDto.setPriceCount(subOrderInfoMapper.sumTotalPrice(shopId));

        Example example2 = new Example(GoodsInfo.class);

        orderTimeDataDto.setGoodsCount(goodsInfoMapper.selectCountByExample(example2));



        return orderTimeDataDto;
    }

    @Override
    public OrderCount getOrderCount(Long shopId) {

        OrderCount  orderCount = new OrderCount();
        List<String> colums = new ArrayList<>();
        List<OrderCountData> orderCountDataList = subOrderInfoMapper.selectOrderStatus(shopId);

        for(OrderCountData count:orderCountDataList){
            if(ConstantKey.UNPAID.equals(count.getName())){
                count.setName("未支付");
            }else if(ConstantKey.INVALID_ORDER.equals(count.getName())){
                count.setName("订单失效");
            }else if(ConstantKey.SEAL_THE_DEAL.equals(count.getName())){
                count.setName("完成交易");
            }else if(ConstantKey.MERCHANDISE_RETURN.equals(count.getName())){
                count.setName("商品退货");
            }

            colums.add(count.getName());
        }
        orderCount.setColumn(colums);
        orderCount.setOrderCountDatas(orderCountDataList);
        return orderCount;
    }

    @Override
    public Map<String, Object> chartCount(Long shopId) {
        Map<String,Object> map = new LinkedHashMap<>();
        int start = DateTimeUtil.dateToTimestampT(DateUtil.beginOfMonth(new Date()));
        int end = DateTimeUtil.dateToTimestampT(DateUtil.endOfMonth(new Date()));
        List<ChartDataDto> chartDataDtos = subOrderInfoMapper.charList(shopId,start,end);
        List<ChartDataDto> chartTDataDtos = subOrderInfoMapper.charListT(shopId,start,end);

        map.put("chart",chartDataDtos);
        map.put("chartT",chartTDataDtos);
        return map;
    }
}
